package com.liuceng.zen.init;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liuceng.zen.lib.gdt.GdtCode;
import com.liuceng.zen.lib.gdt.clients.GdtUtilitiesClient;
import com.liuceng.zen.lib.gdt.vo.ApiBusinessInterest;
import com.liuceng.zen.lib.gdt.vo.ApiIndustryConf;
import com.liuceng.zen.lib.gdt.vo.ApiUtilities;
import com.liuceng.zen.lib.gdt.vo.ApiUtilitiesLocation;
import com.liuceng.zen.lib.gdt.vo.GdtList;
import com.liuceng.zen.lib.gdt.vo.GdtResult;
import com.liuceng.zen.lib.gdt.vo.other.ListJson;

@Service
public class InitUtilities {
  @Autowired
  GdtUtilitiesClient utilitiesClient;

  public List<ApiUtilities> getAreaList() {
    GdtResult<GdtList<ApiUtilities>> res = utilitiesClient.areaList();
    if (GdtCode.CODE_正常 == res.getRet()) {
      GdtList<ApiUtilities> utilities = res.getData();
      return utilities.getList();
    }
    return null;
  }

  public List<ApiUtilities> getCityList() {
    GdtResult<ListJson> res = utilitiesClient.cityList();
    List<ApiUtilities> utilitiesList = new ArrayList<ApiUtilities>();
    ApiUtilities apiUtilities = new ApiUtilities();
    if (GdtCode.CODE_正常 == res.getRet()) {
      ListJson citylist = res.getData();
      String json = citylist.getList().toString();
      String all = json.substring(1, json.length() - 1);
      String[] temp = all.split(",");
      for (int i = 0; i < temp.length; i++) {
        String[] utility = temp[i].split(":");
        apiUtilities.setId(Integer.parseInt(utility[0].substring(1, utility[0].length() - 1)));
        apiUtilities.setName(utility[1].substring(0, utility[1].length()));
        utilitiesList.add(apiUtilities);
      }
      return utilitiesList;
    }
    return null;
  }

  public ApiUtilitiesLocation getLocationList(Integer ids) {
    GdtResult<ApiUtilitiesLocation> res = utilitiesClient.locationList(ids);
    if (GdtCode.CODE_正常 == res.getRet()) {
      ApiUtilitiesLocation aders = res.getData();
      return aders;
    }
    return null;
  }

  public List<ApiBusinessInterest> getBusinessInterest() {
    GdtResult<GdtList<ApiBusinessInterest>> res = utilitiesClient.businessInterest();
    if (GdtCode.CODE_正常 == res.getRet()) {
      GdtList<ApiBusinessInterest> gdtList = res.getData();
      List<ApiBusinessInterest> list = gdtList.getList();
      return list;
    }
    return null;
  }

  public List<ApiIndustryConf> getIndustryConf() {
    GdtResult<GdtList<ApiIndustryConf>> res = utilitiesClient.industryConf();
    if (GdtCode.CODE_正常 == res.getRet()) {
      GdtList<ApiIndustryConf> aders = res.getData();
      List<ApiIndustryConf> list = aders.getList();
      return list;
    }
    return null;
  }

  public List<ApiIndustryConf> getAdIndustryConf() {
    GdtResult<GdtList<ApiIndustryConf>> res = utilitiesClient.adIndustryConf();
    if (GdtCode.CODE_正常 == res.getRet()) {
      GdtList<ApiIndustryConf> aders = res.getData();
      List<ApiIndustryConf> list = aders.getList();
      return list;
    }
    return null;
  }
}
